home *** CD-ROM | disk | FTP | other *** search
/ PsL Monthly 1993 December / PSL Monthly Shareware CD-ROM (December 1993).iso / prgmming / dos / c / oostrng.exe / OOSTRING.CPP < prev    next >
C/C++ Source or Header  |  1992-02-11  |  7KB  |  221 lines

  1. // OOSTRING.CPP
  2. //
  3. // Class library for strings
  4. // Version 1.1
  5. //
  6. // Programmer : John Bernstein
  7. //
  8. //  For file notes, see the project notes sheet.
  9.  
  10. ///////////////////////////////////////////////////////////////////////////
  11.  
  12. #include ".\oostring\oostring.h"
  13. #include <assert.h>
  14.  
  15. ///////////////////////////////////////////////////////////////////////////
  16.  
  17. string::string(char *str)              // char array constructor
  18. {
  19.   length = strlen(str);                // set the length of the string
  20.   size = length + 1;                   // size includes \0
  21.   string_ptr = new char[size];         // allocate space for the string
  22.   strcpy(string_ptr, str);             // copy the string
  23. }  // end of char array conversion
  24.  
  25.  
  26. string::string(char ch)                // character constructor
  27. {
  28.   length = 1;                          // since only 1 character, length is 1
  29.   size = 2;                            // 1 character + null byte
  30.   string_ptr = new char[size];         // allocate space
  31.   string_ptr[0] = ch;                  // put the character in place
  32.   string_ptr[1] = '\0';                // put the null byte on
  33. }  // end of character constructor
  34. ///////////////////////////////////////////////////////////////////////////
  35.  
  36. string::string(const string &src)      // copy constructor
  37. {
  38.   if (src.string_ptr != NULL)          // only copy if there is something
  39.     {
  40.       length = src.length;
  41.       size = length + 1;
  42.       string_ptr = new char[size];
  43.       strcpy(string_ptr, src.string_ptr);
  44.     }
  45.   else
  46.     {
  47.       length = 0;                      // nothing there so set to null
  48.       size = 0;
  49.       string_ptr = NULL;               // and void
  50.     }
  51. }  // end of copy constructor
  52.  
  53. ///////////////////////////////////////////////////////////////////////////
  54.  
  55. string& string::operator=(const string &src)  // assignment operator
  56. {
  57.   if (this != &src)                    // make sure no self assignment
  58.     {
  59.       if ((src.length + 1) > size)     // if not enough space in current string
  60.         {
  61.           delete string_ptr;           // release old memory
  62.           length = src.length;         // copy length
  63.           size = length + 1;
  64.           string_ptr = new char[size]; // allocate space
  65.         }
  66.       else
  67.         length = src.length;           // enough space, so just re-use
  68.       strcpy(string_ptr, src.string_ptr);  // copy string
  69.     }
  70.  
  71.  
  72. //  else   /////// why is this even necessary??????
  73. //    /// a more graceful or error proof way should be found
  74. //    {
  75. //      cout << "Error: Attempted self assignment!\n"
  76. //       << "       program terminated!"
  77. //       << endl;
  78. //      exit(-1);
  79. //    }
  80.  
  81.  
  82.   return (*this);
  83. }  // end of string::operator=
  84.  
  85. ///////////////////////////////////////////////////////////////////////////
  86.  
  87. string operator+(const string &str1, const string &str2)
  88. {
  89.   string temp;                         // temporary storage
  90.  
  91.   temp.length = str1.length + str2.length;      // add the lengths together
  92.   temp.size = temp.length + 1;
  93.   temp.string_ptr = new char[temp.size];        // allocate space
  94.   strcpy(temp.string_ptr, str1.string_ptr);     // copy first string
  95.   strcat(temp.string_ptr, str2.string_ptr);     // append second string
  96.   return (temp);
  97. }  // end of string::operator+
  98.  
  99.  
  100. ///////////////////////////////////////////////////////////////////////////
  101.  
  102. string string::operator+=(const string &str)
  103. {
  104.   *this = *this + str;
  105.   return (*this);
  106. }  // end of operator +=
  107.  
  108. ///////////////////////////////////////////////////////////////////////////
  109.  
  110. string string::operator+()
  111. {
  112.   string temp = *this;                 // allocate new temp space
  113.  
  114.   for (int i = 0; i < temp.length; i++)
  115.     temp.string_ptr[i] = toupper(temp.string_ptr[i]);
  116.   return (temp);
  117. }  // end of unary operator +
  118.  
  119. ///////////////////////////////////////////////////////////////////////////
  120.  
  121. string string::operator-()
  122. {
  123.   string temp = *this;                 // allocate new temp space
  124.  
  125.   for (int i = 0; i < temp.length; i++)
  126.     temp.string_ptr[i] = tolower(temp.string_ptr[i]);
  127.   return (temp);
  128. }  // end of unary operator -
  129.  
  130.  
  131. ///////////////////////////////////////////////////////////////////////////
  132.  
  133. string& string::operator++()
  134. {
  135.   for (int i = 0; i < length; i++)
  136.     string_ptr[i] = toupper(string_ptr[i]);
  137.   return (*this);
  138. }  // end of unary operator ++
  139.  
  140.  
  141. ///////////////////////////////////////////////////////////////////////////
  142.  
  143. string& string::operator--()
  144. {
  145.   for (int i = 0; i < length; i++)
  146.     string_ptr[i] = tolower(string_ptr[i]);
  147.   return (*this);
  148. }  // end of unary operator --
  149.  
  150. ///////////////////////////////////////////////////////////////////////////
  151.  
  152. char& string::operator[](const int index)
  153. {
  154.   assert ((index >= 0) && (index < length));  // make sure no range errors
  155.   return (string_ptr[index]);
  156. }
  157.  
  158. ///////////////////////////////////////////////////////////////////////////
  159.  
  160. ostream& operator<<(ostream &stream, const string &str)
  161. {
  162.   stream << str.string_ptr;
  163.   return (stream);
  164. }  // end of operator <<
  165.  
  166.  
  167. ostream& operator<<(ostream &stream, const string *str)
  168. {
  169.   stream << str->string_ptr;
  170.   return (stream);
  171. }  // end of operator <<
  172.  
  173.  
  174. ///////////////////////////////////////////////////////////////////////////
  175. // WARNING:  As with most stream operator >> functions, care must be
  176. //           taken to pre-allocate enough buffer space.  This function
  177. //           DOES NOT allocate any memory!
  178.  
  179. istream& operator>>(istream& stream, string& str)
  180. {
  181.   assert(str.size > 0);                // make sure there is a buffer to read into
  182.   stream.width(str.size);              // set the max input size = to buffer size
  183.   stream >> str.string_ptr;
  184.   str.length = strlen(str.string_ptr);
  185.   return (stream);
  186. }  // end of operator >>
  187.  
  188.  
  189. int string::setsize(const int newsize)
  190. {
  191.   char* temp;                          // temporary storage for old string
  192.  
  193.   assert(newsize >= 0);                // size MUST be >= 0
  194.   size = newsize;                      // put in the new size
  195.   if (size == 0)                       // 0 size, so set to null condition
  196.     {
  197.       delete string_ptr;
  198.       length = 0;
  199.     }
  200.   else                                 // positive size so re-allocate space
  201.     {
  202.       temp = new char[size];           // allocate the new space
  203.       strncpy(temp, string_ptr, (size - 1));  // copy as much as possible, leaving one space for null
  204.       temp[size - 1] = '\0';           // set the last byte to null
  205.       delete string_ptr;               // release old memory
  206.       string_ptr = temp;               // make temp the new string
  207.       length = strlen(string_ptr);     // set the new length
  208.     }
  209.   return (size);
  210. }  // end of member function setsize
  211.  
  212. void string::getline(istream& stream, const int maxlen)
  213. {
  214.   assert(maxlen > 0);                  // get at least one character
  215.   if (size != 0)                       // is there already something in this string?
  216.     delete string_ptr;                 // yeah, so delete it
  217.   string_ptr = new char[maxlen];       // get storage space
  218.   size = maxlen;                       // set buffer size
  219.   stream.getline(string_ptr, maxlen);  // get line from input stream
  220.   length = strlen(string_ptr);         // set string length
  221. }  // end of member function getline